package com.wys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wys.pojo.Category;
import com.wys.result.R;
import com.wys.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        return categoryService.addCategory(category);
    }

    /**
     *  分页查询
     * @param page 当前页
     * @param pageSize  页面大小
     * @return
     */
    @GetMapping("/page")
    public R<Page> getPage(int page, int pageSize){

        return categoryService.getPage(page,pageSize);
    }

    /**
     *  删除分类
     * @param ids 要删除的ID
     * @return
     */
    @DeleteMapping
    public R<String> deleted(Long ids){

        return categoryService.deleted(ids)? R.success("删除成功!"):R.error("删除失败！");
    }

    /**
     *  修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("分类信息修改成功！");
    }

    /**
     *  根据分类查询，来在添加菜品页面进行展示
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> findCategoryByType(Category category){
        System.out.println(category);
        return categoryService.findCategoryByType(category);
    }
}
